dojo.provide("dojot.group2.TopicList");
dojo.require("dojot.util.DynamicPageList");
dojo.require("dojo.i18n");
dojo.requireLocalization("dojot", "common");
dojo.require("dojo.html");
dojo.declare("dojot.group2.TopicList",null,{
	templateUrl:"",
	reviewNode: null,
	tpl:null,
	urlList:{
		defaultUrl:"",
		boardUrl:"",
		marrowUrl:"",
		userPubUrl:"",
		userRepUrl:""
	},
	defultUrlAry:{
		boardId: "${boardId}",
		currentPage: "${currentPage}",
		itemCountPerPage:"${itemCountPerPage}"
	},
	constructor: function(obj){
		
		this._messages = dojo.i18n.getLocalization("dojot", "common",dojo.locale);
		
		this.reviewNode = obj.reviewNode;
		this.templateUrl = dojo.moduleUrl("dojot.group2", "resources/topicList.html");
		this.tpl = new dojox.dtl.Template(this.templateUrl);
		//订阅一个事件，当有人触发这个事件的时候进行数据渲染,负责数据处理的类，拿到数据后会发布这个事件
		dojo.subscribe("show",dojo.hitch(this,"_render"));
		
		var urlList = {
			defaultUrl:"http://gs.tianji.com/group2-rest-web/jsonp/group/${gid}/bbs/topic/@page/${currentPage},${itemCountPerPage}",
			boardUrl:"http://gs.tianji.com/group2-rest-web/jsonp/group/${gid}/bbs/board/${boardId}/topic/@page/${currentPage},${itemCountPerPage}",
			marrowUrl:"http://gs.tianji.com/group2-rest-web/jsonp/group/${gid}/bbs/topic/@marrow/@page/${currentPage},${itemCountPerPage}",
			userPubUrl:"http://gs.tianji.com/group2-rest-web/jsonp/group/${gid}/bbs/member/${userId}/topic/@page/${currentPage},${itemCountPerPage}",
			userRepUrl:"http://gs.tianji.com/group2-rest-web/jsonp/group/${gid}/bbs/member/${userId}/topic/@comment/@page/${currentPage},${itemCountPerPage}"
		};
		
		//替换url中间的userId参数和gid参数
		//defultUrlAry设置的是我们第一次替换没有被替换的参数，如果不设置默认参数,否则报错
		for(var url in urlList){
			this.urlList[url] = dojo.string.substitute(urlList[url],dojo.mixin(this.defultUrlAry,obj));
		}
		//初始化数据
		this.tplContext = new dojox.dtl.Context();
		var req = {
			url: (obj.url ? this.urlList[url] : null) || this.urlList.defaultUrl
		}
		this.pageList = new dojot.util.DynamicPageList(req);
		//默认显示第一页
		this.pageList.showIndex(1);
		this.tplContext.pageList = this.pageList;

		
		//绑定各个链接的事件,包括分版,精华主题等
		this._bindEvent();	
		dojo.global["showPage"] = dojo.hitch(this,"show");
		
	},
	_bindEvent: function(){
		var that = this;
		dojo.query(".boardHref","content").onclick(dojo.hitch(that,"addEventToObject"));
	},
	addEventToObject: function(evt){		
			var url = dojo.attr(evt.target,"urlType");
			var boardId = (url === "boardUrl") ? dojo.attr(evt.target,"value") : "";
			var boardName = evt.target.innerHTML;
			this.setDataUrl(url,boardId);
			this._setBoardName(boardName);
			this.show(1);
			dojo.stopEvent(evt);
	},
	_render:function(data){
		//拿到数据后，重新初始化分页信息
		console.log("========================render====================");
		this.pageList.pageInfo = data.pageInfo;
		this.pageList.items = data.topicExtends;
		//如果没有回复时间，则把回复时间设置成创建时间
		dojo.forEach(this.pageList.items,function(item){
			if(!item.lastReplyTime){
				item.lastReplyTime = item.createTime;
			}
		});
		this.pageList.initTurnPageInfo();

		//i18n
		var templateString = dojo.string.substitute(this.tpl.render(this.tplContext),{ 
				next: this._messages["next"],
				prev: this._messages["prev"]
		});
		templateString = templateString.replace(/&amp;/g, '&').replace(/&lt;/g,"<").replace(/&gt;/g,">");
		dojo.html.set(this.reviewNode,templateString);
		//this.reviewNode.innerHTML = templateString;
		this._setTopicTotalView()
		this._getUserName();
	},
	setDataUrl: function(urlType,boardId){
		var contentUrl = this.urlList[urlType];
		//如果传过来borad，说明用的是还有boardId的url，所以替换boardId
		if(boardId){
			var obj = {
				boardId:boardId
			}
			contentUrl = dojo.string.substitute(contentUrl,dojo.mixin(this.defultUrlAry,obj));
		}
		console.log("contentUrl,---",contentUrl);
		this.pageList.pageDataStore.setRequestUrl(contentUrl);
	},
	show: function(index){		
		this.pageList.showIndex(index);
		if(dojo.isIE && window.event){
			dojo.stopEvent(window.event);	
		}
	},
	_getUserName:function(){
		initPage();
	},
	_setBoardName: function(boardName){
		dojo.byId("boardNameView").innerHTML = boardName;
	},
	_setTopicTotalView: function(){
		dojo.byId("topicTotalView").innerHTML = this.pageList.pageInfo.itemCount;
	}
})